﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DaoShopOnline
{
    public class ManageCategories
    {
        private ShopOnlineDataContext context;

        public ManageCategories()
        {
            this.context = new ShopOnlineDataContext();
        }

        public IQueryable<Category> GetAllCategories()
        {
            return this.context.Categories.OrderBy(category => category.CategoryName);
        }

        public void EditCategory(Category newCategory)
        {
            Category categoryToEdit = (from cat in this.context.Categories
                                       where cat.CategoryID == newCategory.CategoryID
                                       select cat).SingleOrDefault();
            categoryToEdit.CategoryName = newCategory.CategoryName;
            this.context.SubmitChanges();
        }

        public void DeleteCategory(Category category)
        {
            this.context.Categories.DeleteOnSubmit(category);
            this.context.SubmitChanges();
        }

        public void AddCategory(Category category)
        {
            this.context.Categories.InsertOnSubmit(category);
            this.context.SubmitChanges();
        }
    }
}
